#include "host.h"

void host_init(void)
{
    host_data_init(); 
    host_data_print();
}

void comm_sync(rt_uint16_t *hold_reg_buf)
{
    rt_enter_critical();
    hold_reg_buf[ARG_ID_SV] = host_data->sv;
    hold_reg_buf[ARG_ID_TempAlarmLow] = host_data->tempAlarmLow;
    hold_reg_buf[ARG_ID_TempAlarmHigh] = host_data->tempAlarmHigh;
    hold_reg_buf[ARG_ID_AT] = host_data->atEnable;
    hold_reg_buf[ARG_ID_TECCtrlMode] = host_data->tecCtrlMode;
    hold_reg_buf[ARG_ID_TECPower] = host_data->tecPower;
    hold_reg_buf[ARG_ID_WorkMode] = host_data->workMode;
    hold_reg_buf[ARG_ID_PIDKp] = host_data->pidKp;
    hold_reg_buf[ARG_ID_PIDKi] = host_data->pidKi;
    hold_reg_buf[ARG_ID_PIDKd] = host_data->pidKd;
    hold_reg_buf[ARG_ID_Reserved1] = 0;
    hold_reg_buf[ARG_ID_Reserved2] = 0;
    hold_reg_buf[ARG_ID_Reserved3] = 0;
    hold_reg_buf[ARG_ID_SVOffset] = host_data->svOffset;
    hold_reg_buf[ARG_ID_PVOffset] = host_data->pvOffset;
    hold_reg_buf[ARG_ID_TECOutputDir] = host_data->tecOutputDir;
    hold_reg_buf[ARG_ID_TempSensorType] = host_data->tempSensorType;
    hold_reg_buf[ARG_ID_TempUnit] = host_data->tempUnit;
    hold_reg_buf[ARG_ID_DecimalPointNbr] = host_data->decimalPointNbr;
    hold_reg_buf[ARG_ID_DisplayRangeLow] = host_data->displayRangeLow;
    hold_reg_buf[ARG_ID_DisplayRangeHigh] = host_data->displayRangeHigh;
    hold_reg_buf[ARG_ID_FilterDegree] = host_data->filterDegree;
    hold_reg_buf[ARG_ID_MBSlaverAddr] = host_data->mbSlaverAddr;
    hold_reg_buf[ARG_ID_MBBaudrateID] = host_data->mbBaudrateID;
    hold_reg_buf[ARG_ID_ProgCurrentStep] = host_data->progCurrentStep;
    hold_reg_buf[ARG_ID_ProgTimeUnitAdjust] = host_data->progTimeUnitAdjust;
    hold_reg_buf[ARG_ID_ProgTimeUnitHold] = host_data->progTimeUnitHold;
    hold_reg_buf[ARG_ID_ProgStart] = host_data->progStart;
    hold_reg_buf[ARG_ID_ProgTempAcceptance] = host_data->progTempAcceptance;
    for(rt_uint8_t i = 0; i < 8; i++)
    {
        hold_reg_buf[ARG_ID_ProgStep1AdjustTime + i * 6] = host_data->progList[i].stepAdjustTime;
        hold_reg_buf[ARG_ID_ProgStep1Sv + i * 6] = host_data->progList[i].stepSv;
        hold_reg_buf[ARG_ID_ProgStep1HoldTime + i * 6] = host_data->progList[i].stepHoldTime;
        hold_reg_buf[ARG_ID_ProgStep1Mode + i * 6] = host_data->progList[i].stepMode;
        hold_reg_buf[ARG_ID_ProgStep1NextStepNbr + i * 6] = host_data->progList[i].stepNext->stepNbr;
        hold_reg_buf[ARG_ID_ProgStep1LoopCnt + i * 6] = host_data->progList[i].stepLoopCnt;
    }
    hold_reg_buf[ARG_ID_PowerOnAutoRun] = host_data->powerOnAutoRun;
    hold_reg_buf[ARG_ID_SteadyAlarmRange] = host_data->steadyAlarmRange;
    hold_reg_buf[ARG_ID_TECMaxPower] = host_data->tecMaxPower;
    hold_reg_buf[ARG_ID_MBCrcEnable] = host_data->mbCrcEnable;
    hold_reg_buf[ARG_ID_ErrorCode] = host_data->errorCode;
    hold_reg_buf[ARG_ID_Reserved57] = 0;
    hold_reg_buf[ARG_ID_Reserved58] = 0;
    hold_reg_buf[ARG_ID_Reserved59] = 0;
    hold_reg_buf[ARG_ID_PIDMode] = host_data->pidMode;
    for(rt_uint8_t i = 0; i < 10; i++)
    {
        hold_reg_buf[ARG_ID_TempCalibReal0 + i] = host_data->tempCalibReal[i];
        hold_reg_buf[ARG_ID_TempCalibSet0 + i] = host_data->tempCalibSet[i];
    }
    hold_reg_buf[ARG_ID_TempCalibPointNbr] = host_data->tempCalibPointNbr;
    hold_reg_buf[ARG_ID_Factory] = 0;
    rt_exit_critical();
}

void host_factory(void)
{
    host_data_factory();
    rt_hw_cpu_reset();
}
MSH_CMD_EXPORT_ALIAS(host_factory, factory, reset host data);